< previous page page_362 next page >

Page 362
Structures and DLL Calls
Before continuing further, we must consider one important fact. The vast majority of Visual Basic programmers will never run into the kinds of problems that are discussed through the rest of this tutorial. For most Visual Basic programmers, handling structures will be as easy as the following example, which illustrates the use of the GetClientRect API to retrieve the dimensions of a window's client area:
Type RECT
        Left As Long
        Top As Long
        Right As Long
        Bottom As Long
End Type

Declare Function GetClientRect Lib "user32" _
(ByVal hwnd As Long, lpRect As RECT) As Long

Dim rc As RECT
Call GetClientRect(myHwnd, rc)
Structure parameters (except for the cases noted earlier) are always passed by reference. This causes the address of the start of the structure to be passed to the DLL function.
Unfortunately, there are situations where handling structures becomes very complex. So complex that many of the puzzles in this book relate directly to structure declarations and parameters. The problem areas generally fall into the categories of string handling, variable alignment, and combinations of structures (structures within structures and arrays of structures).
The Most Important Thing to Remember
Your ultimate goal when dealing with structures is to cause Visual Basic to pass as a parameter a pointer to a block of memory that corresponds exactly to the format the DLL function expects to see.
From this you can draw two obvious conclusions:
The most important thing to learn in order to handle structures correctly is how structures are stored in memory and how they are manipulated during the process of a DLL call.

 
< previous page page_362 next page >